<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
    require_once('functions.php'); //Require/include the functions file
    
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['adddir'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Track a Directory'));
    } else {
         
        //Check to see if the form has been submitted by checking if any of the fields have data in them
        if (isset($_POST['dir'])) {
            //Check if the required fields have been filled out
            if (empty($_POST['dir'])) {
                die (errorpage (" Please enter a directory!", 'Error!'));
            }
             
            $directory = $_POST['dir'];
             
            if (!is_dir($directory)) {
                die (errorpage (strip_tags($directory)." is not a vaild directory!", 'Error!'));
            }
            if (substr($directory, -1) !== '/') {
                $directory = "$directory/";
            }
            //Connect to DB using db.php
            $db = new my_db;
             
            // create an array to hold directory list
            $file_dirlist = array();
             
            if ($_POST['subdirectories'] == 'yes') {
                recursive_list($directory);
            } else {
                dirlist($directory);
            }
            template_headtag('Files added to tracker');
            template_header();
            template_left();
            echo "<div class='pageheadertext'>Files added to tracker</div>";
            echo "<div align='center'><table class='table'>";
             
            foreach($file_dirlist as $file2) {
                if ($rowstyle == 'oddrow') {
                    $rowstyle = 'evenrow';
                } else {
                    $rowstyle = 'oddrow';
                }
                echo "<tr class='$rowstyle'>";
                //Clean up vars to make sure they are safe to insert into the DB
                $file2 = mysql_escape_string($file2);
                $file_arr = explode ('/', $file2);
                $count = count($file_arr);
                $filepositon = $count - 1; //Arrays start counting at zero
                $filename = $file_arr[$filepositon];
                array_pop($file_arr);
                $filepath = implode('/', $file_arr);
                $filenotes = "";
                $status = addfile ($filepath, $filename, $filenotes);
                if ($status == 'badfile') {
                    echo "<td align='left'>$filepath$filename</td><td align='left'> File does not exist!</td>";
                } elseif($status == 'already') {
                    echo "<td align='left'>$filepath$filename </td><td align='left'> File is already being tracked!</td>";
                } elseif($status == 'isdir') {
                    echo "<td align='left'>$filepath$filename </td><td align='left'> Can not be tracked, is a directory</td>";
                } elseif($status == 'cannotread') {
                    echo "<td align='left'>$filepath$filename </td><td align='left'> Can not read file, permission denied!</td>";
                } elseif($status == 'added') {
                    echo "<td align='left'>$filepath$filename </td><td align='left'> File added to DB!</td>";
                } else {
                    echo "<td align='left'>$filepath$filename </td><td align='left'> An unknown error has occurred!</td>";
                }
                echo "</tr>";
                 
            }
             
            echo "</table><br /></div>";
            template_footer();
        } else {
            template_headtag('Add New Directory');
            template_header();
            template_left();
            echo "<div class='pageheadertext'>Add New Directory</div><br />";
            echo "<p>Instructions come in here. Must add in CSS and stylize.</p>";
            echo "<br />
                <form method='post' action='adddir.php'>
                <table>
                <tr>
                <td><img src='images/add/folderadd.png' />&nbsp;&nbsp;&nbsp;Directory :</td>
                <td><input type='text' name='dir' size=100 /></td>
                </tr>
                <tr>
                <td><img src='images/add/files.png' />&nbsp;&nbsp;&nbsp;Add files in subdirectories :</td>
                <td><INPUT type='radio' name='subdirectories' value='yes' />Yes &nbsp;&nbsp;&nbsp;<INPUT type='radio' name='subdirectories' value='no' checked />No </td>
                </tr>
                </table>
                
                <br />
                <input type='submit' name='Submit' value='Add Directory' />
                </form>
                ";
            template_footer();
        }
    }
?>